import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginTypeCheck } from '@rsbuild/plugin-type-check';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import { DEPLOY_CONFIG, isProduction } from '../../config/env';

export default defineConfig({
  plugins: [
    pluginReact(),
    pluginTypeCheck(),
    pluginModuleFederation({
      name: 'mainApp', // 主应用名称
      remotes: {
        microApp: `microApp@${DEPLOY_CONFIG.MICRO_APP_URL}`, // 远程微应用
        microApp1: `microApp1@${DEPLOY_CONFIG.MICRO_APP1_URL}`, // 远程微应用
      },
      shared: {
        react: {
          singleton: true,
          eager: true,
          requiredVersion: '^18.2.0',
        },
        'react-dom': {
          singleton: true,
          eager: true,
          requiredVersion: '^18.2.0',
        },
        'react-router-dom': {
          singleton: true,
          eager: true,
          requiredVersion: '^6.8.0',
        },
        antd: {
          singleton: true,
          eager: true,
          requiredVersion: '^5.0.0',
        },
        '@shared/utils': {
          singleton: true,
          eager: true,
        },
      },
    })
  ],
  server: {
    port: 3000,
  },
  html: {
    template: '../../templates/index.html',
  },
  source: {
    entry: {
      index: './src/main.tsx',
    },
    include: [/[\\/]node_modules[\\/]@shared[\\/]pc[\\/]/,/[\\/]node_modules[\\/]@shared[\\/]utils[\\/]/],
    transformImport: [
      {
        libraryName: '@shared',
        libraryDirectory: '',
        style: false,
        camelToDashComponentName: false
      }
    ]
  },
  output: {
    distPath: {
      root: 'dist',
    },
    // 使用assetPrefix设置资源路径前缀
    assetPrefix: isProduction ? './' : '/',
  },
});
